	$(document).ready(function(){
		InitAccount();
	});	
	
        var sended=true;
	
	function updateGame()
	{
                $.ajax({type: 'POST',async:false,  url: $('#url_status').val(),success: function(data)
		{
			try
  			{				
  				var obj = jQuery.parseJSON(data);
                                if(obj.status==1)
                                {
                                   if(sended)
                                     location.reload();
                                }else if(obj.status==2){
                                    $('#user_mess').text(obj.text);
                                    $('#user_mess').show();
                                }
			}catch(err)
  			{
				
			}
			setTimeout("updateGame()",7000);
		}});
		//setTimeout("updateGame()",1000);				
	}
	
        function SendAction(address)
        {
            sended=false;
            $('#representation').hide();
            $.ajax({type: 'POST',async:true,  url: address,success: function(data)
            {                    
                    $('#user_mess').text(data);
                    $('#user_mess').show();
                    alert(data);
                    sended=true;
                    setTimeout("updateGame()",2000);
            }});            
        }
        
        
        function checkGame()
	{
            $.ajax({type: 'POST',async:true,  url: $('#randval').val(),success: function(data)
            {                    
                    if(data=='1')
                    {
                        $('#img_wait').hide();
                        $('#wait_next').show();
                    }
            }}); 
        }
        
	function InitAccount()
	{
		
        if($('#matval').length)
        {
          if($('#matval').val()=='1')
              setTimeout("updateGame()",7000);                    
        }else if($('#randval').length){
              setTimeout("checkGame()",7000);  
        }else{
              setTimeout("updateGame()",7000);
	}
                
		$('.ingame_action').live('click',function(event)
		{
			SendAction($(this).attr('href'));
                        event.preventDefault();
		});
		
		$('.ingame_tree_action').live('click',function(event)
		{
			SendAction($(this).attr('href'));
                        event.preventDefault();
		});
	}	
